clear;clc

EEG = pop_loadset('filepath','D:\Marije\ICA Analyze\CW Sphere Unambiguous Response epochs low freq band passed.set'); 

% %for decomposing set in control components
% EEG = pop_loadset('filepath','D:\Marije\ICA Analyze\CW Sphere Ambiguous Response epochs low freq band passed.set');
% EEG2 = pop_loadset('filepath','D:\Marije\ICA Analyze\CW Sphere unambiguous Response epochs low freq band passed.set');
% unmixing = EEG2.icaweights * EEG2.icasphere;
% amb_act = []; 
% unamb_act = [];
% for i = 1:length(EEG.data(1,1,:));
% amb_act(:,:,i) = unmixing * EEG.data(:,:,i);
% % unamb_act(:,:,i) = unmixing * EEG2.data(:,:,i);
% end
%  
% EEG.icaact = amb_act;
% EEG.icaweights = EEG2.icaweights;
% EEG.icasphere = EEG2.icasphere;

% % % Selection for alpha unambiguous
% % compSelection = {'5','6','11','15','19','33','41','44','46','47','48','51','53','64','72','83','87','88','93','94','95'};
% % par_comp = {'5','6','53','64','72','83','87','88','93','94','44','46','48'}; 
% % occ_comp = {'11','15','19','33','41','51','47'};
% 
% % % Selection for low gamma sphere unambiguous resp
% % compSelection = {'8','9','12','16','24','31','38','41','43','56','58','59','62','68','69','74','79','80','85','87','1','2','3','4','5','6','7','10','11','13','17','19','23','29','50','88'};
% % par_comp = {'8','9','12','16','24','31','38','41','43','56','58','59','62','68','69','74','79','80','85','87'}; 
% % occ_comp = {'1','2','3','4','5','6','7','10','11','13','17','19','23','29','50','88'};
% 
% % % Selection for alpha sphere unambiguous resp
% % para_comp = {'53','83'}; 
% % ipss_comp = {'87','93'}; 
% % ipsi_comp = {'5','6','44','46','48','64','72'}; 
% % occv_comp = {'15', '19'};
% % occd_comp = {'11','33','41','51',};
% % paro_comp = {'12','29','59','62','67'};
% % temi_comp = {'4','30','43','69','70','79','81'};
% % temm_comp = {'9','13','25','55','60','61','63',};
% % tems_comp = {'10','27','38','49','82'};
% % compSelection = {para_comp, ipss_comp, ipsi_comp, occv_comp, occd_comp, paro_comp, temi_comp,  tems_comp, temm_comp};
% % compMat = [para_comp, ipss_comp, ipsi_comp, occv_comp, occd_comp, paro_comp, temi_comp,  tems_comp, temm_comp];
% 
% % % Selection for alpha fh unamb house stim
% para_comp = {'10','92','96'}; 
% ipss_comp = {}; 
% ipsi_comp = {'4','10','22','48','62','64','76','82','83','91','96'}; 
% occv_comp = {'7','44','75'};
% occd_comp = {'28','57','71','75','78'};
% paro_comp = {'34'};
% temi_comp = {'17','24','31','42','84','88'};
% temm_comp = {'3','6','12','19','21','43','51','59','69'};
% tems_comp = {'1','4','14','18','29','35','37','58','64'};
% compSelection = {para_comp,  ipsi_comp, occv_comp, occd_comp, paro_comp, temi_comp,  tems_comp, temm_comp}; %ipss_comp,
% compMat = [para_comp,  ipsi_comp, occv_comp, occd_comp, paro_comp, temi_comp,  tems_comp, temm_comp]; %ipss_comp,
% regions = {'PAR-A','IPS-I','OCC-V','OCC-D', 'PAR-O','TEM-I','TEM-S','TEM-M'}; %'IPS-S', zelfde volgorde als comp_selection!

% % % Selection for cw sphere unamb resp beta
% para_comp = {'45','50','58','73','88','91'}; 
% ipss_comp = {'57','77','81','85'}; 
% ipsi_comp = {'6','10','11','14','26','31','32','33','34','36','37','51','53','62','68'}; 
% occv_comp = {'2','5','21','22','23','62','79'};
% occd_comp = {'1','3','4','12','17','18','20'};
% paro_comp = {'46','61','69'};
% temi_comp = {'7','24','28','29','39','47','48','52','59','60','64','66','71','78','80'};
% temm_comp = {'9','15','19','25','27','35','38','40','43','55','70','72','73','74','82'};
% tems_comp = {'8','13','30','41','43','54','67','75'};
% compSelection = {ipss_comp,para_comp,  ipsi_comp, occv_comp, occd_comp, paro_comp, temi_comp,  tems_comp, temm_comp}; %ipss_comp,
% compMat = [ipss_comp, para_comp,  ipsi_comp, occv_comp, occd_comp, paro_comp, temi_comp,  tems_comp, temm_comp]; %ipss_comp,
% regions = {'IPS-S','PAR-A','IPS-I','OCC-V','OCC-D', 'PAR-O','TEM-I','TEM-S','TEM-M'}; %'IPS-S', zelfde volgorde als comp_selection!
% 
% % % Selection for cw sphere unamb resp theta
% para_comp = {'75'}; 
% ipss_comp = {'95'}; 
% ipsi_comp = {'8','18','19','24','40','50','64','82','83','84'}; 
% occv_comp = {'29','31','51','54','62','79'};
% occd_comp = {'7','28','31','34','35','51','55','88'};
% paro_comp = {'52'};
% % temi_comp = {'7','24','28','29','39','47','48','52','59','60','64','66','71','78','80'};
% % temm_comp = {'9','15','19','25','27','35','38','40','43','55','70','72','73','74','82'};
% % tems_comp = {'8','13','30','41','43','54','67','75'};
% compSelection = {ipss_comp,para_comp,  ipsi_comp, occv_comp, occd_comp, paro_comp};
% compMat = [ipss_comp, para_comp,  ipsi_comp, occv_comp, occd_comp, paro_comp]; 
% regions = {'IPS-S','PAR-A','IPS-I','OCC-V','OCC-D', 'PAR-O'}; % zelfde volgorde als comp_selection!

% % Selection for cw sphere unamb resp low freq
para_comp = {'34','37','60','83'}; 
ipss_comp = {'78','84','85','92'};
ipsi_comp = {'2','21','25','27','38','61','63','64','71','73'};
occv_comp = {'13','28','33','39','44','72'};
occd_comp = {'3','17','19','24','26','29','42'};
paro_comp = {'74','80'};
% temi_comp = {};
% temm_comp = {};
% tems_comp = {};
compSelection = {ipss_comp,para_comp,  ipsi_comp, occv_comp, occd_comp, paro_comp}; %ipss_comp,
compMat = [ipss_comp, para_comp,  ipsi_comp, occv_comp, occd_comp, paro_comp]; %ipss_comp,
regions = {'IPS-S','PAR-A','IPS-I','OCC-V','OCC-D', 'PAR-O'};

% f = figure;
% [ersp,itc,powbase,times,freqs,erspboot,itcboot] = pop_newtimef(EEG, 0, str2num(compMat{1}), [-4000 4000],0,'plotitc','off', 'freqs',[7,13], 'outputformat','plot', 'baseline', [-4000 -2000; 2000 4000]);
% close(f);

figure; ersp_output = [];
for i = 1:length(compMat);
h = subplot(10,10,i);
ersp_output(:,:,i) = pop_newtimef(EEG, 0, str2num(compMat{i}), [-4000 4000],0,'plotitc','off', 'freqs',[4,30], 'outputformat','plot','baseline', [-4000 -2000; 2000 4000]);
title (['Component' compMat{i}]);
end

for i = 1:length(regions)
    comp = compSelection{i};
    paraIdc = [];
    for a = 1:length(comp)
        A = find(strcmp(compMat,comp{a}));
    paraIdc(a,:) = A(1,1);
    end;
    
    ersp_para = NaN(length(ersp_output(:,1,1)),length(ersp_output(1,:,1)),length(comp));
    for j = 1:length(paraIdc(:,1));
    ersp_para(:,:,j) = ersp_output(:,:,paraIdc(j));
    end
av_para = mean(ersp_para,3);
fig = figure; imagesc(av_para, [-2.5 2.5]);
title (['CW - Sphere Unambiguous Response - Selection after ICA - ' regions{i}]);
colorbar
set(gca,'xTick',1:33.3:200); set(gca,'xTickLabel',[-3000, -2000, -1000, 0, 1000, 2000,3000]);
set(gca,'YDir','normal');
% set(gca,'yTick',1:40:161); set(gca,'yTickLabel',[30,40,50,60,70]); 
% set(gca,'yTick',1:4:25); set(gca,'yTickLabel',[7,8,9,10,11,12,13]); 
% set(gca,'yTick',1:4:13); set(gca,'yTickLabel',[4,5,6,7]); 
% set(gca,'yTick',1:4:69); set(gca,'yTickLabel',[13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]); 
freq = [4:1:30];
set(gca,'yTick',1:4:105); set(gca,'yTickLabel',freq); 
saveFigure(fig, ['C:\Users\Marije\Documents\ECoG\ICA\Low freq\'], ['CW - SphereUnambiguousResponse - selected after ica - averaged ersp - ' regions{i}]);
clearvars comp ersp_para av_para A;
end

